import axios from 'axios'
import store from "../store/index.js"

axios.defaults.withCredentials = true;//让ajax携带cookie
axios.defaults.baseURL = "/api"
// axios.defaults.baseURL = nework
axios.defaults.timeout = 50000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// 请求拦截
axios.interceptors.request.use(config => {
  store.state.loeding = true
  setTimeout(()=>{
    store.state.loeding = false
  },5000)
  return config
})
// 响应拦截
axios.interceptors.response.use(config => {
  store.state.loeding = false
  const redirect = config.headers.redirect
  // 后端需要重定向
  if (redirect === 'REDIRECT') {
    const path = config.headers.contextpath
    let win = window;
    while (win !== win.top){
      win = win.top;
    }
    win.location.href = path;
    this.$message.warning(config.headers.mes);
  }
  return config
})
export default axios
